/**
 * Created by Administrator on 2015/11/11 0011.
 */
class NetSend {
    private callback: Function;
    private key;
    private a;
    /**
     * net请求
     * 
     * @param ca ["控制器","操作"] 或 ["操作"] 控制器 farm
     * @param data 发送参数 例：{"p1":"postP1","p2":"postP2"}
     * @param callback
     * @param key 需要储存的信息
     */
    public send(ca: Array<string>, data: Object, callback: Function, key?) {

        if (ca.length === 1) {
            ca.unshift("farm");
        }
        this.a = ca[1];
        data['token'] = platform.token;
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open(CommonData.url + "/" + platform.url_api + ca[0] + "/" + ca[1], egret.HttpMethod.POST);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var sendmsg = "";
        var sendmsgi = 0;
        for (var k in data) {
            if (sendmsgi != 0) {
                sendmsg += "&"
            } else {
                sendmsgi = 1
            }
            sendmsg += k + "=" + data[k];
            // argsArr.push(k + "=" + data[k]);
        }
        request.send(sendmsg);
        // request.send(Utils.urlencode(data));
        this.callback = callback;
        this.key = key;
        // 网络请求加载完成
        request.addEventListener(egret.Event.COMPLETE, this.onPostComplete, this);
        // io发生错误
        request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onPostIOError, this);
        // // 加载进度发生变化
        // request.addEventListener(egret.ProgressEvent.PROGRESS, this.onPostProgress, this);
    }



    /**
     * 网络请求加载完成
     */
    private onPostComplete(event: egret.Event) {
        var request = <egret.HttpRequest>event.currentTarget;
        // console.log("post data : ", request.response);
        var arr = JSON.parse(request.response);
        if (this.a == "isLogin") {
            this.callback && this.callback(arr);
        } else {
            if (arr["code"] == 200)
                this.callback && this.callback(arr.data, arr, this.key);
            else {
                SceneManager.ins.alert(arr["msg"]);
            }
        }
    }



    /**
     * io发生错误
     */
    private onPostIOError(event: egret.IOErrorEvent): void {
        console.log("post error : " + event);
    }



    // 官网方法
    // private onPostComplete(event: egret.Event) {
    //     var request = <egret.HttpRequest>event.currentTarget;
    //     console.log("post data : ", request.response);
    //     var responseLabel = new egret.TextField();
    //     responseLabel.size = 18;
    //     responseLabel.text = "POST response:\n" + request.response.substring(0, 50) + "...";
    //     this.addChild(responseLabel);
    //     responseLabel.x = 300;
    //     responseLabel.y = 70;
    //     this.statusPostLabel.text = "Get POST response!";
    // }
    // private onPostIOError(event: egret.IOErrorEvent): void {
    //     console.log("post error : " + event);
    // }
    // private onPostProgress(event: egret.ProgressEvent): void {
    //     console.log("post progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
    // }


}
